package cn.edu.lit.jsjxy.CourseExample.chapter3;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.lit.jsjxy.CourseExample.R;

public class DialogTestActivity extends Activity implements View.OnClickListener
{
    Button btnNormal, btnList, btnListSingle, btnListMulti, btnProgressH,
            btnProgressCircle, btnCustom, btnTimePicker, btnDatePicker;

    int index;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_test);

        btnNormal = findViewById(R.id.btnNormal);
        btnList = findViewById(R.id.btnList);
        btnListSingle = findViewById(R.id.btnListSingle);
        btnListMulti = findViewById(R.id.btnListMulti);
        btnProgressH = findViewById(R.id.btnProgressH);
        btnProgressCircle = findViewById(R.id.btnProgressCircle);
        btnCustom = findViewById(R.id.btnCustom);
        btnTimePicker = findViewById(R.id.btnTimePicker);
        btnDatePicker = findViewById(R.id.btnDatePicker);

        btnNormal.setOnClickListener(this);
        btnList.setOnClickListener(this);
        btnListSingle.setOnClickListener(this);
        btnListMulti.setOnClickListener(this);
        btnProgressH.setOnClickListener(this);
        btnProgressCircle.setOnClickListener(this);
        btnCustom.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);
        btnDatePicker.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.btnNormal:
                showNormalDialog(this);
                break;

            case R.id.btnList:
                showListDialog();
                break;

            case R.id.btnListSingle:
                showSingleChoiceDialog();
                break;

            case R.id.btnListMulti:
                showMultiChoiceDialog();
                break;


            case R.id.btnProgressH:
                showProgressBarHorizontal();
                break;

            case R.id.btnProgressCircle:
                showProgressBarCircle();
                break;

            case R.id.btnCustom:
                showHalfCustomDialog();
                break;

            case R.id.btnTimePicker:
                showTimePickDialog();
                break;

            case R.id.btnDatePicker:
                showDatePickDialog();
                break;

        }
    }

    private void showDatePickDialog()

    {
        DatePickerDialog dataDialog = new DatePickerDialog(this);
        dataDialog.setCanceledOnTouchOutside(false);
        dataDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener()
        {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
            {
                String dateMsg = "year:" + i + " month:" + (i1 + 1) + " day:" +
                        i2;
                Toast.makeText(DialogTestActivity.this, dateMsg, Toast
                        .LENGTH_SHORT).show();

            }
        });

        dataDialog.show();

    }

    private void showTimePickDialog()
    {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener()
                {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int i, int i1)
                    {
                        String timeStr = "hour: " + i + " minute: " + i1;
                        Toast.makeText(DialogTestActivity.this, timeStr, Toast
                                .LENGTH_SHORT).show();
                    }
                }, hour, minute, true);

        timePickerDialog.show();
    }


    private void showNormalDialog(Context context)
    {

        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.icon);//设置标题的图片
        builder.setTitle("我是对话框");//设置对话框的标题
        builder.setMessage("我是对话框的内容");//设置对话框的内容
        //设置对话框的按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(DialogTestActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(DialogTestActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        dialog =builder.create();

        //dialog = builder.create();
        dialog.show();
    }

    private void showListDialog()
    {
        final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.icon)//设置标题的图片
                .setTitle("列表对话框")//设置对话框的标题
                .setItems(items, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        index = which;
                        Toast.makeText(DialogTestActivity.this, items[which], Toast
                                .LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    private void showSingleChoiceDialog()
    {


        final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.icon)//设置标题的图片
                .setTitle("单选列表对话框")//设置对话框的标题
                .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(DialogTestActivity.this, items[which], Toast
                                .LENGTH_SHORT).show();
                        index = which;
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(DialogTestActivity.this, items[index],
                                Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    private void showMultiChoiceDialog()
    {
        final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};
        final boolean checkedItems[] = {true, false, true, false};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.icon)//设置标题的图片
                .setTitle("多选对话框")//设置对话框的标题
                .setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked)
                    {
                        checkedItems[which] = isChecked;
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        String strChecked = "";
                        for (int i = 0; i < checkedItems.length; i++) {
                            if (checkedItems[i]) {
                                strChecked += i + ",";
                            }
                        }

                        if (strChecked.equals("")) {
                            strChecked = "Nothing is selected!";
                        } else {
                            strChecked = strChecked.substring(0,
                                    strChecked.length() - 1);
                            strChecked = "The following are selected: "
                                    + strChecked;
                        }
                        Toast.makeText(DialogTestActivity.this, strChecked, Toast
                                .LENGTH_SHORT).show();
                        dialog.dismiss();
                    }

                }).create();

        dialog.show();
    }

    private void showProgressBarCircle()
    {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage("正在加载中");
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private void showProgressBarHorizontal()
    {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("正在加载中");
        dialog.setMax(100);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask()
        {
            int progress = 0;
            @Override
            public void run()
            {
                dialog.setProgress(progress += 5);
                if (progress == 100) {
                    timer.cancel();
                    try {
                        Thread.sleep(5000);
                        dialog.dismiss();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, 0, 500);
        dialog.show();
    }

    private void showHalfCustomDialog()
    {
        View view = getLayoutInflater().inflate(R.layout.ccustom_dialog, null);

        final EditText editText = (EditText) view.findViewById(R.id
                .etUsername_dialog);

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.icon)//设置标题的图片
                .setTitle("半自定义对话框")//设置对话框的标题
                .setView(view)
                .setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        String content = editText.getText().toString();
                        Toast.makeText(DialogTestActivity.this, content, Toast.LENGTH_SHORT)
                                .show();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }
}
